//
//  ANBuyCollectionView.m
//  Ants
//
//  Created by ZHM on 16/8/27.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANBuyCollectionView.h"

@interface ANBuyCollectionView ()
@property (nonatomic, weak) UIButton *mengView;
@property (nonatomic, weak) ANBuyCollectionView *selfCopy;
@end

#define optionsViwHeight 44
#define optionsViwAnimationHeight 89

@implementation ANBuyCollectionView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
    }
    return self;
}

+ (instancetype)buyCollectionViewWithViewController:(id)delegate titles:(NSArray *)titles animation:(BOOL)animation{
    UIViewController *vc = (UIViewController *)delegate;
    CGFloat view_H = animation ?optionsViwAnimationHeight : optionsViwHeight;
    ANBuyCollectionView *buyCV = [[ANBuyCollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(ANKeyWindow.frame) - optionsViwHeight, ANKeyWindow.bounds.size.width, view_H)];
    buyCV.delegate = delegate;
    buyCV.titles = titles;
    buyCV.animation = animation;
    if (titles.count > 1) buyCV.animation = NO;
    [buyCV addUpAllChildView];
    [vc.view addSubview:buyCV];
    return buyCV;
}

- (void)addUpAllChildView {
    
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:self.bounds];
    toolbar.barStyle = UIBarStyleDefault;
    [self addSubview:toolbar];
    
    for (int i = 0; i < _titles.count; i++) {
        if (i == _titles.count - 1) {
            if (_animation) {
                [self addButtonWithBackgroundColor:[UIColor clearColor] titleColor:[UIColor orangeColor] title:_titles[i] tag:i + 100];
                [self addButtonWithBackgroundColor:[UIColor clearColor] titleColor:[UIColor blackColor] title:@"取消" tag:i + 101];
                UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, ANscreenW, 2)];
                line.image = [UIImage imageWithStretchableName:@"timeline_card_bottom_line_highlighted"];
                [self addSubview:line];
            } else {
                [self addButtonWithBackgroundColor:[UIColor orangeColor] titleColor:[UIColor whiteColor] title:_titles[i] tag:i + 100];
            }
        } else {
            [self addButtonWithBackgroundColor:[UIColor clearColor] titleColor:[UIColor blackColor] title:_titles[i] tag:i + 100];
        }
    }
}

- (void)addButtonWithBackgroundColor:(UIColor *)backgroundColor titleColor:(UIColor *)titleColor title:(NSString *)title tag:(NSInteger)tag{
    UIButton *btn = [[UIButton alloc] init];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn setBackgroundColor:backgroundColor];
    if (tag == _titles.count - 1 + 100) {
        if (_animation) {
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        } else {
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
        }
    } else {
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    }
    [btn addTarget:self action:@selector(chickBtn:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = tag;
    [self addSubview:btn];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    NSMutableArray *buts = [NSMutableArray array];
    for (id subView in self.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            [buts addObject:(UIButton *)subView];
        }
    }
    for (NSInteger i = buts.count - 1; i >= 0; i--) {
        if (buts.count == 1 || _animation) {
            if (i == 1) {
                UIButton *but = buts[1];
                CGFloat x = 0;
                CGFloat y = 45;
                CGFloat w = self.width;
                CGFloat h = 44;
                but.frame = CGRectMake(x, y, w, h);
            }else {
                UIButton *but = buts[0];
                CGFloat x = 0;
                CGFloat y = 0;
                CGFloat w = self.width;
                CGFloat h = 44;
                but.frame = CGRectMake(x, y, w, h);
            }
        } else {
            if (i == buts.count - 1) {
                UIButton *but = buts[i];
                CGFloat x = self.width - 100;
                CGFloat y = 0;
                CGFloat w = 100;
                CGFloat h = 44;
                but.frame = CGRectMake(x, y, w, h);
            } else {
                UIButton *but = buts[i];
                CGFloat w = ((self.width - 100) - (buts.count - 2)*0.5)/(buts.count - 1);
                CGFloat x = (w + 0.5) * i;
                CGFloat y = 0;
                CGFloat h = 44;
                but.frame = CGRectMake(x, y, w, h);
            }
        }
    }
}

- (void)chickBtn:(UIButton *)btn {
    if (_animation) {
        ANLog(@"_isPopup = %d",_isPopup);
        if (_isPopup) {
            if (btn.tag == 100) {
                //确认
                if ([self.delegate respondsToSelector:@selector(clickConfirmBuyCollectionView:)]) {
                    [self.delegate clickConfirmBuyCollectionView:self];
                }
                [self clickMengView];
            } else {
                //取消
                [self clickMengView];
            }
        } else {
            //弹出显示确认选择按钮
            [self setMangView];
        }
    } else {
        if ([self.delegate respondsToSelector:@selector(buyCollectionView:didClickBtn:)]) {
            [self.delegate buyCollectionView:self didClickBtn:btn.tag - 100];
        }
    }
}
- (void)setMangView {
    //蒙版视图
    UIButton *mengView = [[UIButton alloc] initWithFrame:[UIScreen mainScreen].bounds];
    mengView.backgroundColor = [UIColor blackColor];
    mengView.alpha = 0;
    [mengView addTarget:self action:@selector(clickMengView) forControlEvents:UIControlEventTouchUpInside];
    _mengView = mengView;
    [ANKeyWindow addSubview:mengView];
    [ANKeyWindow addSubview:self];
    _isPopup = YES;
    //动画
    [UIView animateWithDuration:0.25 animations:^{
        UIButton *btn = (UIButton*)[self viewWithTag:100];
        [btn setBackgroundColor:[UIColor orangeColor]];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        mengView.alpha = 0.4;
        self.transform = CGAffineTransformMakeTranslation(0, -45);
    }];
}
//取消选项视图
- (void)clickMengView {
    _isPopup = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.mengView.alpha = 0;
        self.transform = CGAffineTransformMakeTranslation(0, 0);
        UIButton *btn = (UIButton*)[self viewWithTag:100];
        [btn setBackgroundColor:ANToopColor];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    } completion:^(BOOL finished) {
        [self.mengView removeFromSuperview];
        [self removeFromSuperview];
        UIViewController *vc = (UIViewController *)self.delegate;
        [vc.view addSubview:self];
    }];
}

@end
